<template>
  <div class="menu" :style="{ width: isCollapse ? '64px' : '200px' }">
    <div class="menu_header">
      <div class="menu_header_title" v-if="!isCollapse">{{ title }}</div>
      <div class="menu_header_button" @click="isCollapse = !isCollapse">
        <el-icon v-if="!isCollapse">
          <CaretLeft />
        </el-icon>
        <el-icon v-else>
          <CaretRight />
        </el-icon>
      </div>
    </div>
    <div class="menu_content">
      <el-menu
        default-active="1"
        class="el-menu-vertical-demo"
        :collapse="isCollapse"
        @open="handleOpen"
        @close="handleClose"
        :collapse-transition="true"
      >
        <div
          v-permission="item.roles || ['admin']"
          v-for="(item, index) in menuList"
          :key="index"
          :class="isCollapse ? 'el-is-collapse' : ''"
        >
          <el-sub-menu :index="index + ''">
            <template #title>
              <div class="sub-el-item">
                <span class="sub-el-icon">
                  <i :class="item.icon" style="color: #333333" />
                </span>
                <span class="sub-el-text">{{ item.name }}</span>
              </div>
            </template>
            <div
              v-for="(children, number) in item.children"
              :key="number"
              v-permission="children.roles || ['admin']"
            >
              <router-link :to="children.path">
                <el-menu-item :index="children.path">
                  <div class="sub-el-item">
                    <span class="sub-el-icon">
                      <i :class="children.icon" style="color: #333333" />
                    </span>
                    <span class="sub-el-text">{{ children.name }}</span>
                  </div>
                </el-menu-item>
              </router-link>
            </div>
          </el-sub-menu>
        </div>
      </el-menu>
    </div>
  </div>
</template>

<script lang="ts" setup>
import {
  Document,
  Menu as IconMenu,
  Location,
  Setting,
  CaretLeft,
  CaretRight,
} from "@element-plus/icons-vue";
const { t } = useI18n();
const storeActions = useActions("", [
  "getMenuCollapse",
  "getMenuListData",
  "setMenuCollapse",
]);
const title = t("leftMenuTitle");
const handleOpen = () => {};
const handleClose = () => {};
let isCollapse = ref(
  JSON.parse(window.localStorage.getItem("menuCollapse") || "false")
);
watch(isCollapse, (v: boolean) => {
  storeActions.setMenuCollapse(v);
});
storeActions
  .getMenuCollapse()
  .then((res: boolean) => {
    isCollapse.value = res;
  })
  .catch((err: any) => console.error(err));
let menuList = ref<any>([]);
const getMenuListData = () => {
  storeActions
    .getMenuListData()
    .then((res: any) => {
      menuList.value = res.menus;
    })
    .catch((err: any) => console.error(err));
};
onMounted(() => {
  getMenuListData();
});
</script>
<script lang="ts">
import {
  ElMenu,
  ElMenuItem,
  ElSubMenu,
  ElIcon,
  ElMenuItemGroup,
} from "element-plus";
import { defineComponent, onMounted, ref, watch, reactive } from "vue";
import { useI18n } from "vue-i18n";
import { useStore } from "vuex";
import { useActions } from "@/store/useActions";
export default defineComponent({
  components: {
    ElMenu,
    ElMenuItem,
    ElSubMenu,
    ElMenuItemGroup,
    ElIcon,
  },
});
</script>

<style lang="scss">
.menu {
  display: flex;
  flex-direction: column;
}
.menu_header {
  padding: 20px 10px;
  background-color: var(--bg-color);
  display: flex;
  .menu_header_title {
    width: 160px;
    font-size: 14px;
    font-weight: 700;
    text-align: center;
  }
  .menu_header_button {
    cursor: pointer;
    width: 64px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
.menu_content {
  overflow: auto;
  overflow-x: hidden;
  display: block;
  width: 100%;
  height: 100%;
}
.el-menu {
  border: 0;
}
.sub-el-item {
  display: flex;
}
.sub-el-icon {
  vertical-align: middle;
  display: flex;
  align-items: center;
  width: 24px;
}
.el-is-collapse {
  /* 
  .el-sub-menu__title {
    padding: 0;
  } */
  .sub-el-item {
    .sub-el-icon {
      width: 24px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .sub-el-text {
      display: none;
    }
  }
  .el-icon {
    display: none;
  }
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
</style>
